<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>【案例】限定输入内容</title>
    <style rel="stylesheet" type="text/css">
        form :nth-child(3) {
            padding-left: 10px;
            padding-right: 10px;
            padding-top: 5px;
            padding-bottom: 5px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var year = document.getElementsByTagName("input")[0];
            var month = document.getElementsByTagName("input")[1];
            var search = document.getElementsByTagName("input")[2];
            var form= document.getElementById("form");
            search.onclick =function () {
                return checkYear(year)&&checkMonth(month);
            };
            function checkYear(year) {
                if (year.value.match(/^\d\d\d\d$/)){
                    return true;
                }else {
                    year.style.borderColor="red";
                    return false;
                }
            }
            function checkMonth(month) {
                console.log(month.value);
                if (month.value.match(/^[0-9]|(1[0-2])$/)){
                    return true;
                }else {

                    month.style.borderColor="red";
                    return false;
                }
            }
        }
    </script>
</head>
<body>
<form id="form">
    <input type="text">年
    <input type="text">月
    <input type="submit" value="查询">
</form>
</body>
</html>